<template>
  <div class="main">
    <div class="search">
      <el-form :inline="true" :model="search">
        <el-form-item>
          <el-button type="success" size="small" @click="form = { adStatus: true }; dialogShow = true">添加</el-button>
        </el-form-item>
        <el-form-item label="用户姓名">
          <el-input v-model="search.adName" size="small" placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="search.adUserName" size="small" placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="search.adPhone" size="small" placeholder="联系电话" clearable></el-input>
        </el-form-item>
        <!--        <el-form-item label="备注">-->
        <!--          <el-input v-model="search.description" size="small" placeholder="备注" clearable></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item>
          <el-button type="info" size="small" @click="searchClick">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="list">
      <el-table
          :data="data"
          stripe
          style="width: 100%">
        <el-table-column prop="adName" label="用户姓名"></el-table-column>
        <el-table-column prop="adUserName" label="用户名"></el-table-column>
        <el-table-column prop="adPhone" label="联系电话"></el-table-column>
        <el-table-column prop="adStatus" label="状态">
          <template slot-scope="scope">
            {{ scope.row.adStatus == '1' ? '启用' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column prop="description" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="170px">
          <template slot-scope="scope">
            <el-button type="primary" @click="getRowData(scope.row.adId)" dialogShow="true">编辑</el-button>
            <el-button type="danger" @click="del(scope.row.adId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="pageNo"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog
        :title="form.adId ? '修改' : '添加'"
        :visible.sync="dialogShow"
        width="600px"
        @close="dialogClose"
    >
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item prop="adName" label="用户姓名">
          <el-input type="text" placeholder="请输入用户名" v-model="form.adName"/>
        </el-form-item>
        <el-form-item prop="adUserName" label="用户名">
          <el-input type="text" placeholder="请输入用户名" v-model="form.adUserName"/>
        </el-form-item>
        <el-form-item prop="adPassWord" label="密码">
          <el-input type="password" placeholder="请输入密码(默认为123456)" v-model="form.adPassWord"/>
        </el-form-item>
        <el-form-item prop="adPhone" label="联系电话">
          <el-input type="text" placeholder="请输入联系电话" v-model="form.adPhone"/>
        </el-form-item>
        <el-form-item prop="adStatus" label="状态">
          <el-switch
              v-model="form.adStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="0">
          </el-switch>
          <el-form-item label="角色">
<!--            {{roleIds}}-->
<!--            <el-checkbox-group v-model="roleIds">-->
<!--              <el-checkbox v-for="item in roleIds" :key="item.roleId" :label="item.roleId">{{item.roleName}}</el-checkbox>-->
<!--            </el-checkbox-group>-->
            <el-checkbox-group v-model="roleIds">
              <el-checkbox label="9">管理员</el-checkbox>
              <el-checkbox label="11">审核人</el-checkbox>
              <el-checkbox label="10">批准人</el-checkbox>
              <el-checkbox label="12">项目负责人</el-checkbox>
              <el-checkbox label="13">报告校核人</el-checkbox>
              <el-checkbox label="14">用户</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form-item>
        <!--        <el-form-item prop="description" label="备注">-->
        <!--          <el-input type="textarea" placeholder="请输入备注" v-model="form.description"/>-->
        <!--        </el-form-item>-->

        <el-form-item>
          <el-button type="primary" @click="submit">保存</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogShow = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import index from "@/views/Index.vue";

export default {
  name: "Admin",
  computed: {
    index() {
      return index
    }
  },
  data() {
    return {
      search: {},
      dialogShow: false,
      form: {
        adId: '',
        adUserName: '',
        adName: '',
        adPassWord: '',
        adPhone: '',
        adStatus: true,
      },
      roleIds: [],
      data: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      rules: {
        adName: [
          {
            required: true,
            type: 'string',
            message: '用户姓名不能为空',
            trigger: 'blur'
          }
        ],
        adUsername: [
          {
            required: true,
            type: 'string',
            message: '用户名不能为空',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 30,
            message: '用户名长度在3-30位之间',
            trigger: 'blur'
          },
          {
            pattern: /^[0-9A-Za-z_]{3,30}$/,
            message: '用户名只能包含数字、字母、下划线',
            trigger: 'blur'
          },
          // {
          //   validator: (rule, val, cb) => {
          //     let url = `admin/repeat?username=${val}`
          //     if (this.form.id) url += `&id=${this.form.id}`
          //     this.$http.get(url).then(res => {
          //       if (res.code === 200) {
          //         if (res.data) {
          //           return cb(new Error('用户名已存在！'))
          //         }
          //         cb()
          //       }
          //     })
          //   },
          //   trigger: 'blur'
          // }
        ],
        adPassword: [
          {
            required: false,
            type: 'string',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 30,
            message: '密码长度在6-30位之间',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9-*/+.~!@#$%^&*()]{6,30}$/,
            message: '密码只能包含数字、字母、特殊字符',
            trigger: 'blur'
          }
        ],
        adPhone: [
          {
            required: true,
            type: 'string',
            message: '电话不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '电话号码格式不正确',
            trigger: 'blur'
          }
        ],
        description: [
          {
            min: 0,
            max: 800,
            message: '备注长度在3-800位之间',
            trigger: 'blur'
          }
        ]
      },
    }
  },
  mounted() {
    this.getData();
    this.getRoleAll();
  },
  methods: {
    getRoleAll() {
      this.$http.get(`/role/getAll`).then(res => {
        console.log(res)
        // this.roleIds = res.data
        // console.log(res.data)
        // console.log(this.roleIds)
      })
    },
    getData() {
      this.search.page = this.pageNo;
      this.search.limit = this.pageSize;
      this.$http.post(`/reportApproval/admin/list`, this.search).then(res => {
        if (res.code === 0) {
          this.pageSize = res.page.pageSize
          this.pageNo = res.page.currPage
          this.total = res.page.totalCount
          this.data = res.page.list
        }
      })
    },
    // eslint-disable-next-line no-unused-vars
    sizeChange(val) {
      this.pageNo = 1;
      this.pageSize = val;
      this.getData();
    },
    currentChange(val) {
      this.pageNo = val;
      this.getData();
    },
    searchClick() {
      this.pageNo = 1;
      this.getData()
    },
    dialogClose() {
      this.form = {adStatus: true}
      this.$refs['form'].clearValidate()
    },
    // eslint-disable-next-line no-unused-vars
    getRowData(id) {
      this.$http.post(`/reportApproval/admin/info/${id}`).then(res => {
        if (res.code === 0) {
          this.dialogShow = true;
          this.form.adId = res.admin.adId
          this.form.adUserName = res.admin.adUserName
          this.form.adName = res.admin.adName
          this.form.adPassWord = res.admin.adPassWord
          this.form.adPhone = res.admin.adPhone
          this.roleIds = res.admin.roleIds
        }
      })
    },
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.adId) {
            this.form.adUpdated = null;
            this.form.adCreated = null;
            this.$http({
              url: `/reportApproval/admin/update`,
              method: 'post',
              data: {
                'adId': this.form.adId,
                'adUserName': this.form.adUserName,
                'adName': this.form.adName,
                'adPassWord': this.form.adPassWord,
                'adPhone': this.form.adPhone,
                'adStatus': true,
                'roleIds': this.form.roleIds
              }
            }).then(res => {
              if (res.code === 0) {
                this.$Message.success(res.msg)
                this.getData()
                this.dialogShow = false
              }
            })
          } else {
            this.$http({
              url: '/reportApproval/admin/save',
              method: 'post',
              data: {
                'adUserName': this.form.adUserName,
                'adName': this.form.adName,
                'adPassWord': this.form.adPassWord,
                'adPhone': this.form.adPhone,
                'adStatus': true,
                'roleIds': this.roleIds
              }
            }).then(res => {
              console.log(res);
              if (res.code === 0) {
                this.$Message.success(res.msg)
                this.getData()
                this.roleIds = [];
                this.dialogShow = false
              }
            })
          }
        }
      });
    },
    del(id) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(id)
        this.$http.post(`/reportApproval/admin/delete`, [id]).then(res => {
          if (res.code === 0) {
            this.getData()
            this.$Message.success(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>

</style>
